<template>
  <el-dialog v-dialogDrag :title="title" :visible.sync="open" class="dialogStyleSmall" :close-on-click-modal="false" :before-close="cancel">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="岗位名称" prop="postName">
        <el-input v-model="form.postName" placeholder="请输入岗位名称" size="medium" />
      </el-form-item>
      <el-form-item label="岗位编码" prop="postCode">
        <el-input v-model="form.postCode" placeholder="请输入编码名称" size="medium">
          <template slot="prepend">{{ postCode }}</template>
        </el-input>
      </el-form-item>
      <el-form-item label="岗位顺序" prop="postSort">
        <el-input-number v-model.number="form.postSort" controls-position="right" :min="1" size="medium" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <label slot="label" v-html="`备&emsp;&emsp;注`" />
        <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" size="medium" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">

      <el-button size="medium" @click="cancel">取 消</el-button>
      <el-button size="medium" type="primary" :disabled="btnDisabled" @click="submitForm">保 存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getPostByPostId, getPostCode, addPost, updatePost, checkPostNameUnique, checkPostCodeUnique } from '@/api/system/post'
export default {
  name: 'AddDialog',
  components: {},
  data() {
    const checkPostName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('名称不能为空'))
      } else {
        const postId = this.form.postId
        const postName = value
        checkPostNameUnique(postId, postName).then(res => {
          if (res.data === '1') {
            return callback(new Error('名称已经存在'))
          } else {
            callback()
          }
        })
      }
    }
    const checkPostCode = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('不能为空'))
      } else {
        const postId = this.form.postId
        const postCode = this.postCode + value
        checkPostCodeUnique(postId, postCode).then(res => {
          if (res.data === '1') {
            return callback(new Error('岗位编码已经存在'))
          } else {
            callback()
          }
        })
      }
    }
    return {
      postCode: '',
      btnDisabled: false,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {
        postId: '',
        postCode: '',
        postName: '',
        postSort: '0',
        status: '0',
        remark: ''
      },
      // 表单校验
      rules: {
        postName: [
          { required: true, message: '必填', trigger: 'blur' },
          { required: true, validator: checkPostName, trigger: 'blur' }
        ],
        postCode: [
          { required: true, message: '必填', trigger: 'blur' },
          { required: true, validator: checkPostCode, trigger: 'blur' }
        ],
        postSort: [
          { required: true, message: '岗位顺序不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getPostCode()
  },
  methods: {
    getPostCode() {
      getPostCode().then(res => {
        this.postCode = res.data
      })
    },
    handleAdd(title, id, postSort) {
      this.title = title
      this.open = true
      if (id) {
        this.getDetailById(id)
      } else {
        this.form.postSort = postSort
      }
    },
    getDetailById(postId) {
      getPostByPostId(postId).then(res => {
        if (res) {
          const { postId, postName, postSort, remark } = res.data
          const postCode = res.data.postCode.substr(this.postCode.length)
          this.form = { ...this.form, postId, postCode, postName, postSort, remark }
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.btnDisabled = false
      this.form.postId = ''
      this.resetForm('form')
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.btnDisabled = true
          const form = { ...this.form, postCode: this.postCode + this.form.postCode }
          if (this.form.postId) {
            updatePost(form).then(res => {
              if (res) {
                this.msgSuccess('修改成功')
                this.$parent.getList()
                this.cancel()
              }
            }).catch(() => {
              this.btnDisabled = false
            })
          } else {
            addPost(form).then(res => {
              if (res) {
                this.msgSuccess('新增成功')
                this.$parent.getList()
                this.cancel()
              }
            }).catch(() => {
              this.btnDisabled = false
            })
          }
        }
      })
    }
  }
}
</script>

